package com.itany.servlet;

import com.itany.util.CookieUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Author：汤小洋
 * Date：2022-11-07 10:32
 * Description：<描述>
 */
@WebServlet("/cookie")
public class CookieServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /**
         * 向客户端写入cookie
         */
        // Cookie cookie1 = new Cookie("name", "tom");
        // Cookie cookie2 = new Cookie("age", "18");
        // Cookie cookie3 = new Cookie("address", URLEncoder.encode("南京","utf-8")); // 存储中文时需要对中文进行编码
        //
        // cookie2.setMaxAge(7*24*60*60); // 设置有效期为7天
        //
        // // Cookie cookie4 = new Cookie("age", "20");
        //
        // resp.addCookie(cookie1);
        // resp.addCookie(cookie2);
        // resp.addCookie(cookie3);
        // resp.addCookie(cookie4);


        /**
         * 获取客户端传递过来的cookie数据
         */
        // Cookie[] cookies = req.getCookies();
        // for (Cookie c : cookies){
        //     System.out.println(c.getName() +"----------"+URLDecoder.decode(c.getValue(),"utf-8"));
        // }

        /**
         * 使用CookieUtil
         */
        // CookieUtil.addCookie("age","30",7*24*60*60,resp);

        System.out.println(CookieUtil.findByName("age",req));
    }
}
